package com.foreveross.bsl.common.utils.balance.zookeeper2;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.data.Stat;

import com.foreveross.bsl.common.utils.balance.ConfigKeeper;
import com.foreveross.bsl.common.utils.balance.ConfigListener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Set;

/**
 * ZK全局配置程序案例
 *
 * @author hyw
 */
public class ZKConfigKeeper extends ZKCommonKeeper implements ConfigKeeper {

	private static final long serialVersionUID = 1239655944597688769L;
	
    private static final Logger logger = LoggerFactory.getLogger(ZKConfigKeeper.class);
   
    ConfigListener listener;

    
    public ZKConfigKeeper(String serverList, int sessionTimeout) {
        super(serverList, sessionTimeout);
    }

    @Override
    public void setData(String configPath, String data) {
        this.path = configPath;
        try {
        	ZKUtil.buildPath(zk, configPath, CreateMode.PERSISTENT);
            Stat stat = zk.exists(configPath, false);
            zk.setData(configPath, data.getBytes("UTF-8"), stat.getVersion());
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (KeeperException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public void listen(String configPath, ConfigListener listener) {
        this.path = configPath;
        this.listener = listener;
        bindWatcher(configPath);
    }

    public void listenChildren(String configPath, ConfigListener listener) {
        this.path = configPath;
        this.listener = listener;
        bindChildrenWatcher(configPath);
    }

    @Override
    public String getData(String configPath) throws Exception {
        return ZKUtil.getData(zk, configPath);

    }

    public List<String> getChildren(String configPath) throws KeeperException, InterruptedException {
        return ZKUtil.getChildren(zk,configPath);
    }

    public void bindChildrenWatcher(String configPath) {
        try {
        	 ZKUtil.buildPath(zk, configPath, CreateMode.PERSISTENT);
             Stat stat = zk.exists(configPath, false);
            zk.getChildren(configPath, new ZKCommonWatcher(this), stat);
        } catch (KeeperException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void bindWatcher(String configPath) {
        try {
        	ZKUtil.buildPath(zk, configPath, CreateMode.PERSISTENT);
            Stat stat = zk.exists(configPath, false);
            zk.getData(configPath, new ZKCommonWatcher(this), stat);
        } catch (KeeperException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    void process() {
        if (listener != null) {
            try {
				listener.process(getData(path));
			} catch (Exception e) {
				e.printStackTrace();
			}
        } else {
            // logger.warn("监听器为空！");
        }
    }

    @Override
    void afterConnected() {
        logger.info("获取连接。。。");
        process();
    }
    
    // @Override
    // void bindWatcher(Set<String> paths) {
    // // TODO Auto-generated method stub
    //
    // }

    @Override
    void bindWatcher(Set<String> paths) {
        // TODO Auto-generated method stub
    }

}
